package com.game.logic.core.system;

import com.coment.po.ServerInfo;
import com.game.dao.system.ServerInfoDao;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ServerInfoService implements InitializingBean {
   @Value("${spring.application.env}")
   private String env;
   @Value("${spring.application.name}")
   private String name;
   public static ServerInfo serverInfo;
   @Resource
   private ServerInfoDao serverInfoDao;

   public void afterPropertiesSet() {
      try {
         serverInfo = this.serverInfoDao.findById(this.env).orElseGet(() -> {
            ServerInfo serverInfo1 = new ServerInfo(this.env, this.name, System.currentTimeMillis());
            this.serverInfoDao.save(serverInfo1);
            return serverInfo1;
         });
      } catch (Exception e) {
         log.warn("ServerInfoService error", e);
      }

   }

   @Scheduled(
      cron = "0 0 5 * * *"
   )
   public void newDay() {
      this.serverInfoDao.save(serverInfo);
   }
}
